package com.example.myapplication3;

import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";
    private String animaStr="{\n"+
            "\t\"animals\":{\n"+
            "\t\"dog\":[\n"+
            "\t\t{\"name\":\"Rufus\",\"breed\":\"labrador\",\"count\":1,\"twoFeet\":false},\n"+
            "\t\t{\"name\":\"Marty\",\"breed\":\"whippet\",\"count\":1,\"twoFeet\":false}\n"+
            "\t],\n"+
            "\t\"cat\":{\"name\":\"Matilda\"}\n"+
            "}\n"+
"}";
    private String weatherStr="{\n" +
            "\t\"time\": \"2019-06-23 19:27:18\",\n" +
            "\t\"cityInfo\": {\n" +
            "\t\t\"city\": \"广州市\",\n" +
            "\t\t\"cityId\": \"101280101\",\n" +
            "\t\t\"parent\": \"广东\",\n" +
            "\t\t\"updateTime\": \"18:54\"\n" +
            "\t},\n" +
            "\t\"date\": \"20190623\",\n" +
            "\t\"message\": \"Success !\",\n" +
            "\t\"status\": 200,\n" +
            "\t\"data\": {\n" +
            "\t\t\"shidu\": \"92%\",\n" +
            "\t\t\"pm25\": 13.0,\n" +
            "\t\t\"pm10\": 36.0,\n" +
            "\t\t\"quality\": \"优\",\n" +
            "\t\t\"wendu\": \"26\",\n" +
            "\t\t\"ganmao\": \"各类人群可自由活动\",\n" +
            "\t\t\"yesterday\": {\n" +
            "\t\t\t\"date\": \"22\",\n" +
            "\t\t\t\"sunrise\": \"05:42\",\n" +
            "\t\t\t\"high\": \"高温 35.0℃\",\n" +
            "\t\t\t\"low\": \"低温 28.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:15\",\n" +
            "\t\t\t\"aqi\": 30.0,\n" +
            "\t\t\t\"ymd\": \"2019-06-22\",\n" +
            "\t\t\t\"week\": \"星期六\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"多云\",\n" +
            "\t\t\t\"notice\": \"阴晴之间，谨防紫外线侵扰\"\n" +
            "\t\t},\n" +
            "\t\t\"forecast\": [{\n" +
            "\t\t\t\"date\": \"23\",\n" +
            "\t\t\t\"sunrise\": \"05:42\",\n" +
            "\t\t\t\"high\": \"高温 33.0℃\",\n" +
            "\t\t\t\"low\": \"低温 26.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:15\",\n" +
            "\t\t\t\"aqi\": 33.0,\n" +
            "\t\t\t\"ymd\": \"2019-06-23\",\n" +
            "\t\t\t\"week\": \"星期日\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"中到大雨\",\n" +
            "\t\t\t\"notice\": \"出门带雨具，安全最重要\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"24\",\n" +
            "\t\t\t\"sunrise\": \"05:42\",\n" +
            "\t\t\t\"high\": \"高温 28.0℃\",\n" +
            "\t\t\t\"low\": \"低温 26.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:16\",\n" +
            "\t\t\t\"aqi\": 114.0,\n" +
            "\t\t\t\"ymd\": \"2019-06-24\",\n" +
            "\t\t\t\"week\": \"星期一\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"暴雨\",\n" +
            "\t\t\t\"notice\": \"关好门窗，外出避开低洼地\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"25\",\n" +
            "\t\t\t\"sunrise\": \"05:42\",\n" +
            "\t\t\t\"high\": \"高温 31.0℃\",\n" +
            "\t\t\t\"low\": \"低温 26.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:16\",\n" +
            "\t\t\t\"aqi\": 116.0,\n" +
            "\t\t\t\"ymd\": \"2019-06-25\",\n" +
            "\t\t\t\"week\": \"星期二\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"大到暴雨\",\n" +
            "\t\t\t\"notice\": \"雨势转大，在外找好避雨处\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"26\",\n" +
            "\t\t\t\"sunrise\": \"05:43\",\n" +
            "\t\t\t\"high\": \"高温 32.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:16\",\n" +
            "\t\t\t\"aqi\": 120.0,\n" +
            "\t\t\t\"ymd\": \"2019-06-26\",\n" +
            "\t\t\t\"week\": \"星期三\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"雷阵雨\",\n" +
            "\t\t\t\"notice\": \"带好雨具，别在树下躲雨\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"27\",\n" +
            "\t\t\t\"sunrise\": \"05:43\",\n" +
            "\t\t\t\"high\": \"高温 33.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:16\",\n" +
            "\t\t\t\"aqi\": 114.0,\n" +
            "\t\t\t\"ymd\": \"2019-06-27\",\n" +
            "\t\t\t\"week\": \"星期四\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"雷阵雨\",\n" +
            "\t\t\t\"notice\": \"带好雨具，别在树下躲雨\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"28\",\n" +
            "\t\t\t\"sunrise\": \"05:43\",\n" +
            "\t\t\t\"high\": \"高温 34.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:16\",\n" +
            "\t\t\t\"aqi\": 120.0,\n" +
            "\t\t\t\"ymd\": \"2019-06-28\",\n" +
            "\t\t\t\"week\": \"星期五\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"雷阵雨\",\n" +
            "\t\t\t\"notice\": \"带好雨具，别在树下躲雨\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"29\",\n" +
            "\t\t\t\"sunrise\": \"05:44\",\n" +
            "\t\t\t\"high\": \"高温 34.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:16\",\n" +
            "\t\t\t\"ymd\": \"2019-06-29\",\n" +
            "\t\t\t\"week\": \"星期六\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"雷阵雨\",\n" +
            "\t\t\t\"notice\": \"带好雨具，别在树下躲雨\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"30\",\n" +
            "\t\t\t\"sunrise\": \"05:44\",\n" +
            "\t\t\t\"high\": \"高温 34.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:16\",\n" +
            "\t\t\t\"ymd\": \"2019-06-30\",\n" +
            "\t\t\t\"week\": \"星期日\",\n" +
            "\t\t\t\"fx\": \"无持续风向\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"小雨\",\n" +
            "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"01\",\n" +
            "\t\t\t\"sunrise\": \"05:44\",\n" +
            "\t\t\t\"high\": \"高温 34.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:16\",\n" +
            "\t\t\t\"ymd\": \"2019-07-01\",\n" +
            "\t\t\t\"week\": \"星期一\",\n" +
            "\t\t\t\"fx\": \"东南风\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"小雨\",\n" +
            "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"02\",\n" +
            "\t\t\t\"sunrise\": \"05:44\",\n" +
            "\t\t\t\"high\": \"高温 33.0℃\",\n" +
            "\t\t\t\"low\": \"低温 26.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:17\",\n" +
            "\t\t\t\"ymd\": \"2019-07-02\",\n" +
            "\t\t\t\"week\": \"星期二\",\n" +
            "\t\t\t\"fx\": \"东南风\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"小雨\",\n" +
            "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"03\",\n" +
            "\t\t\t\"sunrise\": \"05:45\",\n" +
            "\t\t\t\"high\": \"高温 34.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:17\",\n" +
            "\t\t\t\"ymd\": \"2019-07-03\",\n" +
            "\t\t\t\"week\": \"星期三\",\n" +
            "\t\t\t\"fx\": \"南风\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"小雨\",\n" +
            "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"04\",\n" +
            "\t\t\t\"sunrise\": \"05:45\",\n" +
            "\t\t\t\"high\": \"高温 33.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:17\",\n" +
            "\t\t\t\"ymd\": \"2019-07-04\",\n" +
            "\t\t\t\"week\": \"星期四\",\n" +
            "\t\t\t\"fx\": \"南风\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"小雨\",\n" +
            "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"05\",\n" +
            "\t\t\t\"sunrise\": \"05:45\",\n" +
            "\t\t\t\"high\": \"高温 34.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:17\",\n" +
            "\t\t\t\"ymd\": \"2019-07-05\",\n" +
            "\t\t\t\"week\": \"星期五\",\n" +
            "\t\t\t\"fx\": \"南风\",\n" +
            "\t\t\t\"fl\": \"<3级\",\n" +
            "\t\t\t\"type\": \"小雨\",\n" +
            "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"06\",\n" +
            "\t\t\t\"sunrise\": \"05:46\",\n" +
            "\t\t\t\"high\": \"高温 34.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:17\",\n" +
            "\t\t\t\"ymd\": \"2019-07-06\",\n" +
            "\t\t\t\"week\": \"星期六\",\n" +
            "\t\t\t\"fx\": \"西南风\",\n" +
            "\t\t\t\"fl\": \"3-4级\",\n" +
            "\t\t\t\"type\": \"小雨\",\n" +
            "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"date\": \"07\",\n" +
            "\t\t\t\"sunrise\": \"05:46\",\n" +
            "\t\t\t\"high\": \"高温 33.0℃\",\n" +
            "\t\t\t\"low\": \"低温 27.0℃\",\n" +
            "\t\t\t\"sunset\": \"19:17\",\n" +
            "\t\t\t\"ymd\": \"2019-07-07\",\n" +
            "\t\t\t\"week\": \"星期日\",\n" +
            "\t\t\t\"fx\": \"南风\",\n" +
            "\t\t\t\"fl\": \"3-4级\",\n" +
            "\t\t\t\"type\": \"小雨\",\n" +
            "\t\t\t\"notice\": \"雨虽小，注意保暖别感冒\"\n" +
            "\t\t}]\n" +
            "\t}\n" +
            "}";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Gson gson=new Gson();
               Weather weather=gson.fromJson(weatherStr,Weather.class);
               String type =weather.getData().getForecast().get(0).getType();
              String low =weather.getData().getForecast().get(0).getLow();
                String high =weather.getData().getForecast().get(0).getHigh();
                Log.e(TAG,"天气:"+type+" 低温:"+low+" 高温:"+high);
            }
        });

        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Gson gson=new Gson();
                AnimalData animalData=gson.fromJson(animaStr,AnimalData.class);
                String dogName= animalData.getAnimals().getDog().get(0).getName();
                String catName= animalData.getAnimals().getCat().getName();
                Log.e(TAG,"dog="+dogName+"cat="+catName);
       }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Gson gson=new Gson();
                JsonRootBean jsonRootBean=gson.fromJson(animaStr,JsonRootBean.class);
                String dogName= jsonRootBean.getAnimals().getDog().get(0).getName();
                String catName= jsonRootBean.getAnimals().getCat().getName();
                Log.e(TAG,"dag="+dogName+"cat="+catName);



            }
        });

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Gson gson = new Gson();
                String str1=gson.toJson(1234);
                String str2=gson.toJson("hello");
                String str3=gson.toJson(false);
                String[] arr={"hello","world","welcome"};
                String str4=gson.toJson(arr);
                Log.e(TAG,"str1="+str1+"str2="+str2+"str3="+str3
               +"str4="+str4 );


            }
        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Student student=new Student("张三",20);
                Gson gson = new Gson();
                String str=gson.toJson(student);
                Log.e(TAG,"str="+str);

                JSONObject jsonObject=new JSONObject();

                try {
                    jsonObject.put("age",student.getAge());
                    jsonObject.put("name",student.getName());
                    Log.e(TAG,"jsonObject="+jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str="{\"name\":\"张三\",\"age\":20}";
                Gson gson=new Gson();
                Student student=gson.fromJson(str,Student.class);
                Log.e(TAG,"student="+student);
            }
        });
    }
}
